﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MyLib;

namespace Portal
{
    public partial class SelectCarInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetDate();
            DataBind();
        }

        public void DataBind()
        {

            //查询全部车辆名称和车牌号
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select t.id,t.carname,t.carnumber from WF_CARS t");
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());

            //添加新列存储占用情况
            dt.Columns.Add("Monday", typeof(string));
            dt.Columns.Add("Tuesday", typeof(string));
            dt.Columns.Add("Wednesday", typeof(string));
            dt.Columns.Add("Thursday", typeof(string));
            dt.Columns.Add("Friday", typeof(string));
            dt.Columns.Add("Saturday", typeof(string));
            dt.Columns.Add("Weekday", typeof(string));

            //循环读取每辆车每周的占用情况
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //周一
                StringBuilder strMonday = new StringBuilder();
                strMonday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Monday.Text, dt.Rows[i]["ID"]);
                DataTable dtMonday = DataAccess.Instance("BizDB").ExecuteDataTable(strMonday.ToString());
                if (dtMonday.Rows.Count > 0)
                {

                    dt.Rows[i]["Monday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Monday"] = "可用";
                }
                //周二
                StringBuilder strTuesday = new StringBuilder();
                strTuesday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Tuesday.Text, dt.Rows[i]["ID"]);
                DataTable dtTuesday = DataAccess.Instance("BizDB").ExecuteDataTable(strTuesday.ToString());
                if (dtTuesday.Rows.Count > 0)
                {

                    dt.Rows[i]["Tuesday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Tuesday"] = "可用";
                }

                //周三
                StringBuilder strWednesday = new StringBuilder();
                strWednesday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Wednesday.Text, dt.Rows[i]["ID"]);
                DataTable dtWednesday = DataAccess.Instance("BizDB").ExecuteDataTable(strWednesday.ToString());
                if (dtWednesday.Rows.Count > 0)
                {

                    dt.Rows[i]["Wednesday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Wednesday"] = "可用";
                }

                //周四
                StringBuilder strThursday = new StringBuilder();
                strThursday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Thursday.Text, dt.Rows[i]["ID"]);
                DataTable dtThursday = DataAccess.Instance("BizDB").ExecuteDataTable(strThursday.ToString());
                if (dtWednesday.Rows.Count > 0)
                {

                    dt.Rows[i]["Thursday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Thursday"] = "可用";
                }

                //周五
                StringBuilder strFriday = new StringBuilder();
                strFriday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Friday.Text, dt.Rows[i]["ID"]);
                DataTable dtFriday = DataAccess.Instance("BizDB").ExecuteDataTable(strFriday.ToString());
                if (dtFriday.Rows.Count > 0)
                {

                    dt.Rows[i]["Friday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Friday"] = "可用";
                }

                //周六
                StringBuilder strSaturday = new StringBuilder();
                strSaturday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Saturday.Text, dt.Rows[i]["ID"]);
                DataTable dtSaturday = DataAccess.Instance("BizDB").ExecuteDataTable(strSaturday.ToString());
                if (dtSaturday.Rows.Count > 0)
                {

                    dt.Rows[i]["Saturday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Saturday"] = "可用";
                }

                //周日
                StringBuilder strWeekday = new StringBuilder();
                strWeekday.AppendFormat(@"select * from wf_cars a
left join proc_usecar b on b.carid = a.id
inner join incidents c on rtrim(c.processname)= ltrim(b.processname) and c.incident=b.incident
where to_date('{0}','yyyy-mm-dd')
between to_date(b.usestart,'yyyy-mm-dd') and to_date(b.useend,'yyyy-mm-dd') and a.id='{1}'
and c.status=1", Weekday.Text, dt.Rows[i]["ID"]);
                DataTable dtWeekday = DataAccess.Instance("BizDB").ExecuteDataTable(strWeekday.ToString());
                if (dtWeekday.Rows.Count > 0)
                {

                    dt.Rows[i]["Weekday"] = "已占用";
                }
                else
                {

                    dt.Rows[i]["Weekday"] = "可用";
                }
            }
            RepeaterCar.DataSource = dt;
            RepeaterCar.DataBind();
        }

        public void GetDate()
        {

            string Week = DateTime.Now.DayOfWeek.ToString();
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            switch (Week)
            {

                case "Monday":
                    Monday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 2).ToString();
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 3).ToString();
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 4).ToString();
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 5).ToString();
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 6).ToString();
                    break;
                case "Tuesday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Tuesday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 2).ToString();
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 3).ToString();
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 4).ToString();
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 5).ToString();
                    break;
                case "Wednesday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 2).ToString();
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Wednesday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 2).ToString();
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 3).ToString();
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 4).ToString();
                    break;
                case "Thursday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 3).ToString();
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 2).ToString();
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Thursday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 2).ToString();
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 3).ToString();
                    break;
                case "Friday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 4).ToString();
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 3).ToString();
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 2).ToString();
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Friday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 2).ToString();
                    break;
                case "Sunday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 5).ToString();
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 4).ToString();
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 3).ToString();
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 2).ToString();
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Saturday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    Weekday.Text = year + "-" + month + "-" + (DateTime.Now.Day + 1).ToString();
                    break;
                case "Weekday":
                    Monday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 6).ToString();
                    Tuesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 5).ToString();
                    Wednesday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 4).ToString();
                    Thursday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 3).ToString();
                    Friday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 2).ToString();
                    Saturday.Text = year + "-" + month + "-" + (DateTime.Now.Day - 1).ToString();
                    Weekday.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    break;
            }
        }

        protected void RepeaterCar_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //dinMain.Visible = false;
            //divCarDetail.Visible = true;
            string id = e.CommandArgument.ToString();
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from WF_CARS t where t.id='{0}'", id);
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
            lblCarName.Text = dt.Rows[0]["CARNAME"].ToString();
            lblCarNo.Text = dt.Rows[0]["CARNUMBER"].ToString();
            lblCarRemark.Text = dt.Rows[0]["CARREMARK"].ToString();
            lblCarType.Text = dt.Rows[0]["CARTYPE"].ToString();
            lblPeopleNum.Text = dt.Rows[0]["PEOPLENUM"].ToString();
            lblCarId.Text = dt.Rows[0]["CARID"].ToString();
            lblEnginedID.Text = dt.Rows[0]["ENGINEID"].ToString();
            lblRegisterDate.Text = dt.Rows[0]["REGISTERDATE"].ToString();
        }
        //[System.Web.Services.WebMethod()]
        public static string GetCarDetail(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from WF_CARS t where t.id='{0}'", id);
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
            //lblCarName.Text = dt.Rows[0]["CARNAME"].ToString();
            //lblCarNo.Text = dt.Rows[0]["CARNUMBER"].ToString();
            //lblCarRemark.Text = dt.Rows[0]["CARREMARK"].ToString();
            //lblCarType.Text = dt.Rows[0]["CARTYPE"].ToString();
            //lblPeopleNum.Text = dt.Rows[0]["PEOPLENUM"].ToString();
            //lblCarId.Text = dt.Rows[0]["CARID"].ToString();
            //lblEnginedID.Text = dt.Rows[0]["ENGINEID"].ToString();
            //lblRegisterDate.Text = dt.Rows[0]["REGISTERDATE"].ToString();

            return "111";
        }
    }
}